package com.enoram.gwt.ui;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class DesktopView extends Composite implements DesktopDisplay{

	interface Images extends ClientBundle{
		ImageResource deskitem();
	}
	
	private FlexTable panel = new FlexTable();
	private Panel topPanel = new VerticalPanel();
	private Images images = GWT.create(Images.class);
	private ArrayList<DeskItem> deskItems = new ArrayList<DeskItem>();
	private Grid grid = new Grid(2,3);
	private Presenter presenter;
	public DesktopView(){
		initWidget(panel);
		
		//panel.setHeight("200");
//		panel.setWidth("300");
//		
		panel.setStyleName("desktop");
		
		panel.setWidget(0,0, topPanel);
	//	panel.addSouth(new Label("Bottom"), 150);
		panel.setWidget(1,0,grid);
		
		grid.setCellSpacing(20);
		grid.setCellPadding(10);
		//put the empty one
		
		grid.setWidget(0, 0, new DeskItem("<empty>", images.deskitem(), new Token("")));
		grid.setWidget(0, 1, new DeskItem("<empty>", images.deskitem(), new Token("")));
		grid.setWidget(0, 2, new DeskItem("<empty>", images.deskitem(), new Token("")));
		
		grid.setWidget(1, 0, new DeskItem("<empty>", images.deskitem(), new Token("")));
		grid.setWidget(1, 1, new DeskItem("<empty>", images.deskitem(), new Token("")));
		grid.setWidget(1, 2, new DeskItem("<empty>", images.deskitem(), new Token("")));
		
	}
	
	@Override
	public void add(DeskItem deskItem){
		
		deskItems.add(deskItem);
		
		int col=0, row=0, size = deskItems.size();
		col = (size-1)%3;
		if(size<=3){
			row=0; 
		}else{
			row=1; 
		}
		
		grid.setWidget(row, col, deskItem);
		
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		
	}
	
	public void addTop(Widget topWidget){
		topPanel.add(topWidget);
	}
	
}

